package com.zzxx.exam.ui;

import com.zzxx.exam.entity.*;
import com.zzxx.exam.service.ExamService;
import com.zzxx.exam.service.ExamServiceImpl;
import com.zzxx.exam.service.IdOrPwdException;

import javax.swing.*;
import java.awt.*;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class ClientContext {
    private WelcomeWindow welcomeWindow;
    private LoginFrame loginFrame;
    private MenuFrame menuFrame;
    private ExamFrame examFrame;
    private MsgFrame msgFrame;
    private ExamServiceImpl examService;
    private EntityContext entityContext;
    private User user;
    private Timer timer = new Timer();
    public void show(){
        welcomeWindow.setVisible(true);
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                welcomeWindow.setVisible(false);
                loginFrame.setVisible(true);
            }
        },2000);
    }

    //登录验证
    public void login() {
        try {
            int id = Integer.parseInt(loginFrame.getId());
            String password = new String(loginFrame.getPwd());
            user = examService.login(id, password);
            //进入菜单界面,并将登录界面隐藏
            menuFrame.updateView(user);
            menuFrame.setVisible(true);
            loginFrame.setVisible(false);
            //将user信息传给考试界面


        } catch (IdOrPwdException e) {
            loginFrame.showMessage(e.getMessage());
        }catch (NumberFormatException e){
            loginFrame.showMessage("用户名格式有误!");
        }
    }

    private QuestionInfo currentQuestion;
    private ExamInfo examInfo;
    //开始考试
    public void start() {
        examInfo = examService.start();
        QuestionInfo questionInfo = examService.getQuestion(0);
        currentQuestion = questionInfo;
        //进入考试界面,并将菜单界面隐藏
        //examFrame.updateView(user);

        examFrame.updateExamInfo(examInfo);
        examFrame.updateQuestionInfo(questionInfo);
        examFrame.setVisible(true);
        menuFrame.setVisible(false);
        timeSet();

    }
    //读取考试规则
    public void rulesRead() {
        String rules = examService.getMsg();
        //更新msgFrame
        msgFrame.showMsg(rules);
        msgFrame.setVisible(true);
    }

    public void prev(){
        List<Integer> list = examFrame.getUserAnswers();
        //1.保存用户当前题目的答案
        if(currentQuestion.getQuestionIndex()>0) {
            examService.saveUserAnswers(currentQuestion.getQuestionIndex(),list);
            //2.获得上一题对应的题目信息
            currentQuestion = examService.getQuestion(currentQuestion.getQuestionIndex()-1);
            examFrame.updateQuestionInfo(currentQuestion);
        }
    }

    public void next(){
        List<Integer> list = examFrame.getUserAnswers();
        //1.保存当前题目的用户答案 或者 显示当前这道题的用户的答案
        //System.out.println(list);//上一题的答案打印
        examService.saveUserAnswers(currentQuestion.getQuestionIndex(), list);
        //2.获得下一题对应的题目信息
        if(currentQuestion.getQuestionIndex()<19) {
            currentQuestion = examService.getQuestion(currentQuestion.getQuestionIndex() + 1);
            examFrame.updateQuestionInfo(currentQuestion);
        }
    }
    //交卷
    public void send() {
        //保存用户做过的最后一题的答案
        List<Integer> list = examFrame.getUserAnswers();
        examService.saveUserAnswers(currentQuestion.getQuestionIndex(), list);
        int score = examService.examOver();
        JOptionPane.showMessageDialog(examFrame,"考试成绩为:"+score);
        //回到菜单界面
        menuFrame.setVisible(true);
        examFrame.setVisible(false);
        timer.cancel();
    }

    public void timeSet(){
        long end = System.currentTimeMillis() + examInfo.getTimeLimit()*60*1000;
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                long now = System.currentTimeMillis();
                long left = end - now;
                left -= 1000;
                long hour = left/1000/60/60;
                long minute = left/1000/60 - hour*60;
                long second = left/1000 - minute*60 - hour*60*60;

                examFrame.updateTime(hour,minute,second);
                if(left == 0){
                    send();
                }
            }
        },1,1000);
    }

    public void cancel() {
        System.exit(0);
    }

    public void setWelcomeWindow(WelcomeWindow welcomeWindow) {
        this.welcomeWindow = welcomeWindow;
    }

    public void setLoginFrame(LoginFrame loginFrame) {
        this.loginFrame = loginFrame;
    }

    public void setExamFrame(ExamFrame examFrame) {
        this.examFrame = examFrame;
    }

    public void setExamService(ExamServiceImpl examService) {
        this.examService = examService;
    }

    public void setMenuFrame(MenuFrame menuFrame) {
        this.menuFrame = menuFrame;
    }

    public void setEntityContext(EntityContext entityContext) {
        this.entityContext = entityContext;
    }

    public void setMsgFrame(MsgFrame msgFrame) {
        this.msgFrame = msgFrame;
    }

    public void exit(JFrame source){
        int i = JOptionPane.showConfirmDialog(source, "确定要离开吗?");
        if(i == JOptionPane.OK_OPTION){
            System.exit(0);
        }
    }


}
